<template>
  <div>
    <el-dialog
      :title="title"
      width="600px"
      :visible.sync="dialogVisible"
      :before-close="handleClose"
      :close-on-click-modal="false"
    >
      <div class="dialogBody">
        <el-form
          :model="params"
          :rules="rules"
          ref="ruleForm"
          label-position="left"
          label-width="100px"
          size="small"
        >
          <el-form-item label="社团名称：" prop="name">
            <el-input v-model="params.name" placeholder="请输入"></el-input>
          </el-form-item>
          <el-form-item label="社长姓名：" prop="uid">
            <el-autocomplete
              popper-class="my-autocomplete"
              v-model="params.userName"
              :fetch-suggestions="querySearchAsync"
              placeholder="请输入"
              @select="handleSelect"
            >
              <template slot-scope="{ item }">
                <div v-if="item.id == -1">无匹配结果</div>
                <div v-else>
                  <div class="name">{{ item.userName }}</div>
                  <!-- <div class="addr">联系电话：{{ item.mobile }}</div>
                <div class="addr">居住房屋：{{ item.house.name }}</div>
                <div class="addr">身份证号：{{ item.idNo }}</div> -->
                </div>
              </template>
            </el-autocomplete>
            <div class="userInfo" v-if="infoShow">
              <div class="addr">联系电话：{{ userInfo.mobile }}</div>
              <div class="addr">居住房屋：{{ userInfo.house.name }}</div>
              <div class="addr">身份证号：{{ userInfo.idNo }}</div>
            </div>
          </el-form-item>
          <el-form-item label="社团介绍：" prop="introduction">
            <el-input
              v-model="params.introduction"
              type="textarea"
              :autosize="{ minRows: 3 }"
              size="small"
            ></el-input>
          </el-form-item>

          <el-form-item label="上传封面:" prop="pic">
            <UploadPhoto
              @onSuccess="onSuccess"
              :fileList="params.pic"
            ></UploadPhoto>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleClose">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import UploadPhoto from "@/components/UploadPhoto.vue";

import { addClub, clubDetail } from "@/api/club.js";
import { userList } from "@/api/activity.js";

export default {
  name: "",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    clubId: {
      type: Number,
      default: "",
    },
  },
  data() {
    return {
      title: "新增",
      params: {
        name: "",
        introduction: "",
        pic: [],
        userName: "",
        uid: "",
        organizer_name: "",
      },
      userInfo: {
        house: {},
      },
      infoShow: false,
      rules: {
        name: [{ required: true, message: "请输入社团名称", trigger: "blur" }],
        introduction: [
          { required: true, message: "请输入社团介绍", trigger: "blur" },
        ],
        pic: [{ required: true, message: "请上传封面", trigger: "change" }],
        uid: [{ required: true, message: "请选择社长姓名", trigger: "change" }],
      },
    };
  },

  components: {
    UploadPhoto,
  },

  computed: {},

  beforeMount() {},

  mounted() {
    if (this.clubId) {
      this.title = "编辑";
      this.getInfo();
    }
  },

  methods: {
    getInfo() {
      clubDetail({ id: this.clubId }).then((res) => {
        if (res.code == 200) {
          console.log(res.data, "------data");

          this.params.name = res.data.name;
          this.params.introduction = res.data.introduction;
          this.params.userName = res.data.userName;
          this.params.organizer_name = res.data.organizer_name;
          this.params.uid = res.data.organizer_uid;
          // this.params.organizer_name = res.data.organizer_name;
          this.params.pic = res.data.pic.split(",").map((item) => {
            return {
              name: "",
              url: item,
            };
          });
        }
      });
    },
    //获取上传的图片地址
    onSuccess(val) {
      this.params.pic = val;
    },
    async querySearchAsync(queryString, cb) {
      if (queryString && queryString.length > 0) {
        this.input = queryString;
        try {
          const data = await userList({
            keywords: this.params.userName,
          }); //search定义在data里
          // 赋值给建议列表，渲染到页面
          let list = data.data;
          // 如果list.length等于0，则代表没有匹配到结果。手动给list添加一条提示信息
          if (list.length == "0") {
            list.push({
              id: "-1",
              value: "无匹配结果",
            });
            // 调用 callback 返回建议列表的数据
            cb(list);
          } else {
            // list = list.map((item) => {
            //   return {
            //     value: `${item.userName}`,
            //     id: `${item.id}`,
            //   };
            // });
            // 调用 callback 返回建议列表的数据
            cb(list);
          }
        } catch (error) {
          console.log(error);
        }
      } else {
        cb([]);
      }
    },
    handleSelect(val) {
      this.params.userName = val.userName;
      this.params.organizer_name = val.userName;
      this.params.uid = val.id;
      this.userInfo = val;
      this.infoShow = true;
    },
    onSubmit(formName) {
      console.log("++++++++", this.params);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //处理提交字段
          let params = {
            ...this.params,
          };
          console.log(this.params.pic);
          if (this.params.pic.length > 0) {
            params.pic = this.params.pic.map((item) => item.url).join(",");
          }
          if (this.clubId) params.id = this.clubId;
          //删除不必传入的字段
          delete params.userName;
          //新增/编辑
          addClub(params).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.$emit("handleClose", true);
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleClose() {
      this.$emit("handleClose");
    },
  },

  watch: {},
};
</script>
<style lang="scss" scoped>
.dialogBody {
  /deep/.flexItem {
    display: flex;
    .el-form-item {
      flex: 1;
    }
    .el-input__inner {
      width: 260px;
    }
    .el-range-editor.el-input__inner {
      width: 290px;
    }
  }
}
</style>
